﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace TestApplication
{
	public class DALEFHelper : TimedObjectBase
	{
		private Stopwatch _timer = new Stopwatch();

		public void Select()
		{
			using (Acme.Sales.EFDAL.SalesEntities context = new Acme.Sales.EFDAL.SalesEntities())
			{
				_timer.Reset();
				_timer.Restart();
				var q = (from x in context.Customer
								 select x).ToList();
				_timer.Stop();
				System.Diagnostics.Debug.WriteLine(_timer.Elapsed.TotalMilliseconds);
				this.OnComplete(new TimedEventArgs("EF DAL Select All", (long)_timer.Elapsed.TotalMilliseconds));
			}

			using (Acme.Sales.EFDAL.SalesEntities context = new Acme.Sales.EFDAL.SalesEntities())
			{
				_timer.Reset();
				_timer.Restart();
				var w = (from x in context.Customer
								 where x.FirstName == "John"
								 select x).ToList();
				_timer.Stop();
				System.Diagnostics.Debug.WriteLine(_timer.Elapsed.TotalMilliseconds);
				this.OnComplete(new TimedEventArgs("EF DAL Select By First Name", (long)_timer.Elapsed.TotalMilliseconds));
			}

			using (Acme.Sales.EFDAL.SalesEntities context = new Acme.Sales.EFDAL.SalesEntities())
			{
				_timer.Reset();
				_timer.Restart();
				var e = (from x in context.Customer
								 where x.CreatedDate > new DateTime(2010, 1, 1)
								 select x).ToList();
				_timer.Stop();
				System.Diagnostics.Debug.WriteLine(_timer.Elapsed.TotalMilliseconds);
				this.OnComplete(new TimedEventArgs("EF DAL Select By CreatedDate", (long)_timer.Elapsed.TotalMilliseconds));

			}
		}

		public void Insert()
		{
			Acme.Sales.EFDAL.AuditModifer am = new Acme.Sales.EFDAL.AuditModifer("cdavis");
			using (Acme.Sales.EFDAL.SalesEntities context = new Acme.Sales.EFDAL.SalesEntities(am))
			{
				_timer.Reset();
				_timer.Restart();
				for (int ii = 0; ii < TesterClass.RECORD_COUNT; ii++)
				{
					Acme.Sales.EFDAL.Customer newItem = new Acme.Sales.EFDAL.Customer();
					newItem.Address = "123 Elm Street";
					newItem.City = "Atlanta";
					newItem.Phone = "999-999-9999";
					newItem.FirstName = "John";
					newItem.LastName = "Doe";
					context.AddItem(newItem);
				}
				context.SaveChanges();
				this.OnComplete(new TimedEventArgs("EF DAL Insert", (long)_timer.Elapsed.TotalMilliseconds)); _timer.Stop();
				System.Diagnostics.Debug.WriteLine(_timer.Elapsed.TotalMilliseconds);
			}
		}

		public void Delete()
		{
			using (Acme.Sales.EFDAL.SalesEntities context = new Acme.Sales.EFDAL.SalesEntities())
			{
				_timer.Reset();
				_timer.Restart();
			
				//Select all
				var q = (from x in context.Customer
								 select x).ToList();

				foreach (var a in q)
				{
					context.DeleteObject(a);
				}

				context.SaveChanges();

				_timer.Stop();
				System.Diagnostics.Debug.WriteLine(_timer.Elapsed.TotalMilliseconds);
				this.OnComplete(new TimedEventArgs("EF DAL Delete All", (long)_timer.Elapsed.TotalMilliseconds));
			}

		}

		public void Update()
		{
		}


	}
}
